package cn.initcap.designpattern.principle;


import java.lang.reflect.InvocationTargetException;
import org.springframework.cglib.beans.BeanCopier;

/**
 * @author initcap
 * @date Created in 2/22/19 1:06 PM.
 */
public class TestBeanUtils {

    /**
     * 这里需要注意：Apache的拷贝和Spring的拷贝参数的前后位置意思是不一样的(正好相反);
     * 其中org.apache.commons.beanutils.BeanUtils.copyProperties(b,a);代表把a拷贝给b
     * org.springframework.beans.BeanUtils.copyProperties(a1,b1);代表把a1拷贝给b1
     *
     * @param args
     * @throws InvocationTargetException
     * @throws IllegalAccessException
     */
    public static void main(String[] args) throws InvocationTargetException, IllegalAccessException {
        // 测试apache的BeanUtils
        BeanOne beanOne = new BeanOne();
        beanOne.setA("beanOne");
        beanOne.setB("beanTwo");
        BeanTwo beanTwo = new BeanTwo();
        long start = System.currentTimeMillis();
        org.apache.commons.beanutils.BeanUtils.copyProperties(beanTwo, beanOne);
        long end = System.currentTimeMillis();
        System.out.println("【Apache拷贝结果】: " + beanTwo + "【使用耗时】:" + (end - start));

        // 测试Spring的BeanUtils
        BeanOne beanOne1 = new BeanOne();
        beanOne1.setA("beanOne1");
        beanOne1.setB("beanTwo1");
        BeanTwo beanTwo1 = new BeanTwo();
        long start1 = System.currentTimeMillis();
        org.springframework.beans.BeanUtils.copyProperties(beanOne1, beanTwo1);
        long end1 = System.currentTimeMillis();
        System.out.println("【Spring拷贝结果】: " + beanTwo1 + "【使用耗时】: " + (end1 - start1));

        // 测试Cglib的BeanCopier
        BeanOne beanOne2 = new BeanOne();
        beanOne2.setA("beanOne2");
        beanOne2.setB("beanOne2");
        BeanTwo beanTwo2 = new BeanTwo();
        long start2 = System.currentTimeMillis();
        final BeanCopier copier = BeanCopier.create(BeanOne.class, BeanTwo.class, false);
        copier.copy(beanOne2, beanTwo2, null);
        long end2 = System.currentTimeMillis();
        System.out.println("【Cglib拷贝结果】: " + beanTwo2 + "【使用耗时】: " + (end2 - start2));
    }

}
